<?php
#
#
# Copyright TEDECO, 2009. http://tedeco.fi.upm.es
#
# This file is part of kumenya.
#
# Kumenya is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Kumenya is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
?>
<?php
//include("pdf/class.ezpdf.php");
include("html2fpdf/html2fpdf.php");

// Editable variables.
$Host = "localhost";
$User = "kumenyaapp";
$Password = "EfJ7i49c3v11";
$DBName = "kumenya_matriculacion";

$student = $_POST["student"];
$career = $_POST["career"]; 
$select_bulletin = "SELECT personas.Nombre, personas.Apellidos, asignaturas.Facultad, asignaturas.NombreAsignatura, matriculasasignaturas.NotaFinal, asignaturas.Curso, asignaturas.Ponderacion, matriculas.Anio FROM (((personas RIGHT JOIN alumnos ON alumnos.Uid = personas.Uid) RIGHT JOIN matriculas on matriculas.UidAlumno = alumnos.Uid) RIGHT JOIN  matriculasasignaturas ON matriculasasignaturas.UidMatricula = matriculas.Uid) RIGHT JOIN asignaturas ON asignaturas.Uid = matriculasasignaturas.UidAsignatura WHERE (personas.CodigoPersona = \"".$student."\") AND (matriculasasignaturas.UidAsignatura = ANY (SELECT Uid FROM asignaturas WHERE Carrera = ".$career.")) ORDER BY matriculas.Anio ASC, asignaturas.Curso DESC, asignaturas.NombreAsignatura ASC";
$select_career = "SELECT carrera FROM carreras WHERE uid = ".$career;
// End editable variables.

// Connect to DB
$link = mysql_connect ($Host, $User, $Password) or die('Could not connect: ' . mysql_error());
mysql_select_db($DBName) or die('Could not select database');
$result = mysql_query($select_bulletin);

$rows = mysql_num_rows($result);
if ($rows)
{
    $row = mysql_fetch_row($result);
    $student_name = $row[0].", ".$row[1];
    
    // Name of the faculty
    $select_faculty = "SELECT facultad FROM facultades WHERE uid = ".$row[2];
    $result2 = mysql_query($select_faculty);
    $row2 = mysql_fetch_row($result2);
    $faculty = $row2[0];
    
    // Name of the career
    $result3 = mysql_query($select_career);
    $row3 = mysql_fetch_row($result3);
    $career = $row3[0];    
}

//Generate html string
function Print_table($res)
{
   $table = "";
   $rows = mysql_num_rows($res);

   if (!$rows) $table = $table."<div style=\"text-align:center\"><H2>Table is empty.</H2></div>";
   else
   {
      $table = $table."<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>";
      $table = $table."<tr>";
      $table = $table."<th width=460>Subject</th><th>Mark</th><th>Course</th><th>Ponderation</th><th>Year</th>";
      $table = $table."</tr>";
      $numberfields = mysql_num_fields($res);
      while ($row = mysql_fetch_row($res))
      {
         $table = $table."<tr>";
         for ($i=3; $i<$numberfields; $i++)
         {
            if ($i == 5) $table = $table.'<td align="center">'.(string)(int)$row[$i]."</td>";
            elseif ($i == 3) $table = $table."<td>".$row[$i]."</td>";
            elseif ($row[$i] != "") $table = $table.'<td align="center">'.$row[$i]."</td>";
            else $table = $table.'<td align="center">&nbsp;</td>';
         }
         $table = $table."</tr>";
      }
      $table = $table."</TABLE>";
   }
   return $table;
}

if ($rows)
{
    mysql_data_seek($result, 0);
    
    $html =
    '<html>
    <head>
    <title>Bulletin from '.$faculty.'</title>
    </head>
    <body>
    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=1>
        <tr>
            <td width=560><p style="font-size:large; font-weight:bold"><strong>'.$career.' Bulletin</strong></p><br />
            </td>
            <td rowspan="4"><img src="university.png" alt="University" width="150" height="150"/></td>
        </tr>
        <tr>
            <td><p style="font-size:small; font-weight:bold">'.$faculty.'</p></td>
        </tr>
    </TABLE>
    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
        <tr> 
            <td>Student : <strong>'.$student_name.'</strong><br />
        </tr>    
        <tr> 
            <td>Student Code : '.$student.'</td>
        </tr>    
    </TABLE><br /><br />'
    .Print_table($result).
    '<p style="text-align:right; font-size:10pt">'.date('l, d. F Y').'</p>
    </body>
    </html>';
    
    $pdf = new HTML2FPDF();
    $pdf -> AddPage();
    $pdf -> WriteHTML($html);
    $pdf -> Output('bulletin_'.$student.'.pdf', D);
}
else 
{
    include("../commons/clases.php");
    include("../commons/www.php");
    include(Accion . "students_list_aux.php");
    
    Interfaz::Cabecera(_("Student") . " " . _(" ") . " " . _("List"));
    
    Interfaz::AbrirCuerpo(FALSE);
    
    Interfaz::ProximaPantalla ();
    
    Interfaz::Botones ();
    Interfaz::Formulario (Pagina. "students_list.php","back");
    Interfaz::BotonDeEnvio(_("Back"));
    
    echo '<br />Student code not valid.<br /><br />';
    Interfaz::Fin();
    
    exit;
}
?>

